<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{ text }">
        {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <LabNotifyModal ref="registerModal" @success="handleSuccess"></LabNotifyModal>
    <TableModal @register="registerItemsModal" @success="handleSuccess"></TableModal>
    <TableModalAudit @register="registerAuditModal" @success="handleSuccess"></TableModalAudit>
    <LabExamModal @register="registerModalExam" @success="handleSuccess"></LabExamModal>
  </div>
</template>

<script lang="ts" name="notify-labNotify" setup>
  import { ref, reactive, onMounted } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns } from './LabNotify.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, update } from './LabNotify.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import LabNotifyModal from './components/LabNotifyModal.vue';
  import {  useRoute } from 'vue-router';
  import TableModal from '../items_check/LabCheckItemsFrom.vue'; //实验form
  import TableModalAudit from '../items_check/LabAuditItemsFrom.vue'; //实验form
  import LabExamModal from '../exam/LabUserExamForm.vue';
  import { queryByExamId } from '../exam/LabUserExam.api';
  import { useModal } from '/@/components/Modal';
  import { defHttp } from '/@/utils/http/axios';
  import { message } from 'ant-design-vue';


  const route = useRoute();
  const formRef = ref();
  const queryParam = reactive<any>({});
  const toggleSearchStatus = ref<boolean>(false);
  const registerModal = ref();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '消息通知',
      api: list,
      columns,
      canResize: false,
      useSearchForm: false,
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      showIndexColumn: true,
      tableSetting: {
        // 是否显示刷新按钮
        redo: false,
        // 是否显示尺寸调整按钮
        size: false,
        // 是否显示字段调整按钮
        setting: false,
        // 是否显示全屏按钮
        fullScreen: false,
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '消息通知',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });
  const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] =
    tableContext;
  const labelCol = reactive({
    xs: { span: 24 },
    sm: { span: 7 },
  });
  const wrapperCol = reactive({
    xs: { span: 24 },
    sm: { span: 16 },
  });

  //注册多个model，分别处理打开不同消息对应的处理窗口
  // const [registerItemsModal, { openModal }] = useModal();
  const [registerItemsModal, { openModal: openModal1, closeModal: closeModal1 }] = useModal();
  const [registerAuditModal, { openModal: openModal2, closeModal: closeModal2 }] = useModal();
  const [registerModalExam, { openModal: openModal3, closeModal: closeModal3 }] = useModal();

  // const [registerAuditModal, { openModal }] = useModal();

  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }

  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    update({ id: record.id });
    reload();
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(record);
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  async function handleTodo(record: Recordable) {
    update({ id: record.id });
    if ('实验' === record.notTitlle) {
      //实验模块
      defHttp.get({ url: '/items/labTaskAllot/getOne?itemsId=' + record.taskId + '&entrustId=' + record.entrustId }).then((record) => {
        openModal1(true, {
          record,
          disabled: false,
        });
      });
    } else if ('实验复核' === record.notTitlle) {
      defHttp.get({ url: '/items/labTaskAllot/getOne?itemsId=' + record.taskId + '&entrustId=' + record.entrustId }).then((record) => {
        openModal2(true, {
          record,
          disabled: false,
        });
      });
    } else if ('考核' === record.notTitlle) {
      const paper = await queryByExamId({ examId: record.entrustId });
      console.log(paper);
      if (paper != null) {
        const state = paper.state;
        if (state == '已结束') {
          message.error('本次考试已完成，不能再次考试！');
        } else {
          defHttp.get({ url: '/exam/labExam/queryById?id=' + record.entrustId }).then((record) => {
            openModal3(true, {
              id: record.id,
              disabled: false,
            });
          });
        }
      } else {
        defHttp.get({ url: '/exam/labExam/queryById?id=' + record.entrustId }).then((record) => {
          openModal3(true, {
            id: record.id,
            disabled: false,
          });
        });
      }
    }
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      // {
      //   label: '编辑',
      //   onClick: handleEdit.bind(null, record),
      // },
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '去处理',
        onClick: handleTodo.bind(null, record),
      },
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }

  /**
   * 重置
   */
  function searchReset() {
    formRef.value.resetFields();
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }

  // 在组件挂载后获取路由参数并执行操作
  onMounted(() => {
    //从首页进来的
    if (route.query.yn === 'true') {
      let noticeList = JSON.parse(localStorage.getItem('noticeList'));
      // handleDetail(noticeList);
      handleTodo(noticeList);
    }
  });
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }

    .query-group-cust {
      width: calc(50% - 15px);
      min-width: 100px !important;
    }

    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }
</style>
